----------------------
-- health&power Bar
-- 鼠标提示状态条增强.自定义材质,显示文字.
----------------------
local _G = getfenv(0);
local GameTooltip =_G.GameTooltip;
local CreateFrame = CreateFrame;

local Icetip_HealthBar = GameTooltipStatusBar;

Icetip_BarTexture = {
	"Interface\\Addons\\Icetip\\Images\\bar1",
	"Interface\\Addons\\Icetip\\Images\\bar2",
	"Interface\\Addons\\Icetip\\Images\\bar3",
	"Interface\\Addons\\Icetip\\Images\\bar4",
	"Interface\\Addons\\Icetip\\Images\\bar5",
	"Interface\\Addons\\Icetip\\Images\\bar6",
	"Interface\\Addons\\Icetip\\Images\\bar7",
	"Interface\\Addons\\Icetip\\Images\\bar8",
	"Interface\\Addons\\Icetip\\Images\\bar9",
	"Interface\\Addons\\Icetip\\Images\\bar10",
	"Interface\\Addons\\Icetip\\Images\\bar11",
	"Interface\\Addons\\Icetip\\Images\\bar12",
	"Interface\\Addons\\Icetip\\Images\\bar13",
	"Interface\\Addons\\Icetip\\Images\\bar14",
	"Interface\\Addons\\Icetip\\Images\\bar15",
	"Interface\\TargetingFrame\\UI-TargetingFrame-BitFull",
}

local function HealthGradient(perc)
	local r1, g1, b1
	local r2, g2, b2
	if perc <= 0.5 then
		perc = perc * 2
		r1, g1, b1 = 1, 0, 0
		r2, g2, b2 = 1, 1, 0
	else
		perc = perc * 2 - 1
		r1, g1, b1 = 1, 1, 0
		r2, g2, b2 = 0, 1, 0
	end
	return r1 + (r2-r1)*perc, g1 + (g2-g1)*perc, b1 + (b2-b1)*perc
end

function Icetip:Healbar_RePostion()
	Icetip_HealthBar:ClearAllPoints();

	if IcetipDB.StatusBar.tipHealthBarPoint == "TOP" then
		Icetip_HealthBar:SetPoint("BOTTOMLEFT", GameTooltip, "TOPLEFT", 4, 0)
		Icetip_HealthBar:SetPoint("BOTTOMRIGHT", GameTooltip, "TOPRIGHT", -4, 0)
		Icetip_HealthBar:SetOrientation("HORIZONTAL");
	elseif IcetipDB.StatusBar.tipHealthBarPoint == "BOTTOM" then
		Icetip_HealthBar:SetPoint("TOPLEFT", GameTooltip, "BOTTOMLEFT", 4, 0);
		Icetip_HealthBar:SetPoint("TOPRIGHT", GameTooltip, "BOTTOMRIGHT", -4, 0);
		Icetip_HealthBar:SetOrientation("HORIZONTAL");
	end
end

function Icetip:Healthbar_OnShow()
	Icetip_HealthBar:SetStatusBarTexture(IcetipDB.StatusBar.tipBarTexture);--texture
	Icetip_HealthBar:SetHeight(IcetipDB.StatusBar.tipBarHeight);
	Icetip:Healbar_RePostion();

	HealthBarText = Icetip_HealthBar:CreateFontString(nil, "ARTWORK");
	HealthBarText:SetPoint("CENTER");
	HealthBarText:SetTextColor(1, 1, 1);
	HealthBarText:SetFont(IcetipDB.StatusBar.tipBarFont, IcetipDB.StatusBar.tipBarFontSize, OUTLINE)
	
	Icetip:Healbar_OnUpdate()
end

function Icetip:Healbar_OnUpdate(hp, maxhp)
	if (hp or maxhp) then
		local value
		if maxhp == 0 then
			value = 0
		else
			value = hp/maxhp
		end
		Icetip_HealthBar:SetMinMaxValues(0, 1);
		Icetip_HealthBar:SetValue(value)
		Icetip_HealthBar:SetStatusBarColor(HealthGradient(value));

		if IcetipDB.StatusBar.tipHealthBarText then
			if hp == 0 then
				HealthBarText:SetText("");
			else
				HealthBarText:SetText(hp.." / "..maxhp)
			end
		end
	end
end

-----powerbar
function Icetip:PowerBar_RePostion()
	Icetip_PowerBar:SetHeight(IcetipDB.StatusBar.tipBarHeight)
	Icetip_PowerBar:ClearAllPoints();
	if IcetipDB.StatusBar.tipPowerBarPoint == "TOP" then
		if GameTooltipStatusBar and IcetipDB.StatusBar.tipHealthBarPoint == "TOP" then
			Icetip_PowerBar:SetPoint("BOTTOMLEFT", GameTooltipStatusBar, "TOPLEFT", 0, 4);
			Icetip_PowerBar:SetPoint("BOTTOMRIGHT", GameTooltipStatusBar, "TOPRIGHT", 0, 4);
			Icetip_PowerBar:SetOrientation("HORIZONTAL");
		else
			Icetip_PowerBar:SetPoint("BOTTOMLEFT", GameTooltip, "TOPLEFT", 4, 0);
			Icetip_PowerBar:SetPoint("BOTTOMRIGHT", GameTooltip, "TOPRIGHT", -4, 0);
			Icetip_PowerBar:SetOrientation("HORIZONTAL");
		end
	elseif IcetipDB.StatusBar.tipPowerBarPoint == "BOTTOM" then
		if GameTooltipStatusBar and IcetipDB.StatusBar.tipHealthBarPoint == "BOTTOM" then
			Icetip_PowerBar:SetPoint("TOPLEFT", GameTooltipStatusBar, "BOTTOMLEFT", 0, -4);
			Icetip_PowerBar:SetPoint("TOPRIGHT", GameTooltipStatusBar, "BOTTOMRIGHT", 0, -4);
			Icetip_PowerBar:SetOrientation("HORIZONTAL");
		else
			Icetip_PowerBar:SetPoint("TOPLEFT", GameTooltip, "BOTTOMLEFT", 4, 0);
			Icetip_PowerBar:SetPoint("TOPRIGHT", GameTooltip, "BOTTOMRIGHT", -4, 0);
			Icetip_PowerBar:SetOrientation("HORIZONTAL");
		end
	end
end


function Icetip:PowerBar_OnShow()
	if not Icetip_PowerBar then
		Icetip_PowerBar = CreateFrame("StatusBar", "Icetip_PowerBar_Bar", GameTooltip);
		Icetip_PowerBar:SetStatusBarTexture(IcetipDB.StatusBar.tipBarTexture);
		Icetip:PowerBar_RePostion()

		PowerBarText = Icetip_PowerBar:CreateFontString(nil, "ARTWORK");
		PowerBarText:SetPoint("CENTER");
		PowerBarText:SetTextColor(1, 1, 1);
		PowerBarText:SetFont(IcetipDB.StatusBar.tipBarFont, IcetipDB.StatusBar.tipBarFontSize, OUTLINE);
	end

	if not GameTooltip:GetUnit() then
		Icetip_PowerBar:Hide()
		return
	end

	Icetip_PowerBar:Show()
		

	Icetip:PowerBar_OnUpdate()
end

function Icetip:PowerBar_OnUpdate(unitPowerType, mp, maxmp)
	if not Icetip_PowerBar then
		return
	end

	if unitPowerType and  mp and maxmp then
		local value
		if maxmp == 0 then
			value = 0
		else
			value = mp/maxmp	
		end
		Icetip_PowerBar:SetMinMaxValues(0, 1);
		Icetip_PowerBar:SetValue(value)

		if (IcetipDB.StatusBar.tipPowerBar) then
			if (unitPowerType == 0) then
			--法力值
				Icetip_PowerBar:SetStatusBarColor(ManaBarColor[0].r, ManaBarColor[0].g, ManaBarColor[0].b, 0.8);
			elseif unitPowerType == 1 then
			--怒气
				Icetip_PowerBar:SetStatusBarColor(ManaBarColor[1].r, ManaBarColor[1].g, ManaBarColor[1].b, 0.8);
			elseif unitPowerType == 2 then
			--集中值
				Icetip_PowerBar:SetStatusBarColor(ManaBarColor[2].r, ManaBarColor[2].g, ManaBarColor[2].b, 0.8);
			elseif unitPowerType == 3 then
			--能量
				Icetip_PowerBar:SetStatusBarColor(ManaBarColor[3].r, ManaBarColor[3].g, ManaBarColor[3].b, 0.8);
			end
			Icetip_PowerBar:Show()
		else
			Icetip_PowerBar:Hide()
		end

		if IcetipDB.StatusBar.tipPowerBarText then
			if mp == 0 then
				PowerBarText:SetText("");
			else
				PowerBarText:SetText(mp.." / "..maxmp)
			end
		end
	end
end